from django.db import models
from valar.models.core import VModel


class ProductionBatch(VModel):
    """生物航空煤油生产批次模型"""
    
    # Meta信息
    batch_name = models.CharField(max_length=255, verbose_name="批次/任务名称", blank=True, null=True)
    start_date = models.DateField(verbose_name="工作开始日期", null=True, blank=True)
    end_date = models.DateField(verbose_name="工作结束日期", null=True, blank=True)
    
    # 输入数据 - 屏幕1
    production_load = models.FloatField(verbose_name="生产负荷（0-1）", default=0, null=True)
    restaurant_waste_oil_mass_t = models.FloatField(verbose_name="餐饮废油输入总量（吨）", default=0, null=True)
    dmds_mass_t = models.FloatField(verbose_name="DMDS总量（吨）", default=0, null=True)
    new_hydrogen_mass_t = models.FloatField(verbose_name="新氢输入量（吨）", default=0, null=True)
    recycled_hydrogen_mass_t = models.FloatField(verbose_name="循环氢总量（吨）", default=0, null=True)
    nitrogen_mass_t = models.FloatField(verbose_name="氮气总量（吨）", default=0, null=True)
    recycled_bio_jet_fuel_mass_t = models.FloatField(verbose_name="生物柴油循环量（吨）", default=0, null=True)
    working_hours = models.FloatField(verbose_name="工作持续时间（小时）", default=0, null=True)

    # 输入数据 - 屏幕2
    bio_jet_fuel_mass_t = models.FloatField(verbose_name="生物航煤质量（吨）", default=0, null=True)
    bio_naphtha_mass_t = models.FloatField(verbose_name="生物石脑油质量（吨）", default=0, null=True)
    tail_gas_mass_t = models.FloatField(verbose_name="外排气质量（吨）", default=0, null=True)
    h2o_mass_t = models.FloatField(verbose_name="H2O质量（吨）", default=0, null=True)
    light_contaminated_oil_mass_t = models.FloatField(verbose_name="轻污油专搪（吨）", default=0, null=True)
    
    # 输入数据 - 屏幕3
    recycled_water_mass_t = models.FloatField(verbose_name="循环水消耗量（吨）", default=0, null=True)
    demineralized_water_mass_t = models.FloatField(verbose_name="除盐水消耗量（吨）", default=0, null=True)
    condensate_mass_t = models.FloatField(verbose_name="凝结水（吨）", default=0, null=True)
    electricity_kwh = models.FloatField(verbose_name="电（kWh）", default=0, null=True)
    steam_mass_t = models.FloatField(verbose_name="1.0MPa蒸汽（吨）", default=0, null=True)
    fuel_gas_mass_t = models.FloatField(verbose_name="燃料气（吨）", default=0, null=True)
    
    # 计算结果字段
    e_raw_kg = models.FloatField(verbose_name="原料带入排放（kg CO2）", default=0, null=True, blank=True)
    e_energy_kg = models.FloatField(verbose_name="能耗排放（kg CO2）", default=0, null=True, blank=True)
    e_aux_kg = models.FloatField(verbose_name="辅助材料带入排放（kg CO2）", default=0, null=True, blank=True)
    e_total_kg = models.FloatField(verbose_name="总排放（kg CO2）", default=0, null=True, blank=True)
    carbon_footprint_kg_per_t = models.FloatField(verbose_name="碳足迹（kg CO2/t 产品）", default=0, null=True, blank=True)
    
    # 质量平衡校验字段
    input_total_mass = models.FloatField(verbose_name="合计入方量（吨）", default=0, null=True, blank=True)
    output_total_mass = models.FloatField(verbose_name="合计出方量（吨）", default=0, null=True, blank=True)
    mass_balance_error = models.FloatField(verbose_name="质量平衡误差（吨）", default=0, null=True, blank=True)
    
    class Meta:
        verbose_name = '生产批次'
        verbose_name_plural = '生产批次'

    def __str__(self):
        return f"{self.batch_name or '未命名批次'} - {self.start_date}"


class EmissionFactors(VModel):
    """排放因子模型"""
    
    name = models.CharField(max_length=255, unique=True, verbose_name="因子名称")
    value = models.FloatField(verbose_name="排放因子值")
    unit = models.CharField(max_length=50, verbose_name="单位")
    category = models.CharField(max_length=100, verbose_name="类别", blank=True, null=True)
    description = models.TextField(verbose_name="描述", blank=True, null=True)
    
    class Meta:
        verbose_name = '排放因子'
        verbose_name_plural = '排放因子'

    def __str__(self):
        return f"{self.name} - {self.value} {self.unit}"


class CalculationLog(VModel):
    """计算日志模型"""
    
    batch = models.ForeignKey(ProductionBatch, on_delete=models.CASCADE, verbose_name="生产批次")
    calculation_type = models.CharField(max_length=50, verbose_name="计算类型")
    input_data = models.JSONField(verbose_name="输入数据", null=True, blank=True)
    calculation_result = models.JSONField(verbose_name="计算结果", null=True, blank=True)
    error_message = models.TextField(verbose_name="错误信息", blank=True, null=True)
    calculation_time = models.DateTimeField(auto_now_add=True, verbose_name="计算时间")
    
    class Meta:
        verbose_name = '计算日志'
        verbose_name_plural = '计算日志'

    def __str__(self):
        return f"{self.batch.batch_name} - {self.calculation_type} - {self.calculation_time}"

